package com.itheima.service.impl.checkgroup;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.checkgroup.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.checkgroup.CheckGroupService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 张钊煌
 * @since 2021/11/4 11:26
 */

@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> allCheckGroup = checkGroupDao.findAll();
        return allCheckGroup;
    }

    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup byId = checkGroupDao.findById(id);
        return byId;
    }

    @Override
    public List<CheckItem> findAllItem() {
        List<CheckItem> allItem = checkGroupDao.findAllItem();
        return allItem;
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        List<Integer> itemIds = checkGroupDao.findCheckItemIdsByCheckGroupId(id);
        return itemIds;
    }

    /**
     * 添加检查组对象，添加检查组与检查项关联
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    public void addCheckGroup(CheckGroup checkGroup,Integer[] checkitemIds) {
        checkGroupDao.addCheckGroup(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        if (checkitemIds !=null&&checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("GroupId",checkGroupId);
                map.put("ItemId",checkitemId);
                checkGroupDao.addCheckGroupRelation(map);
            }
        }
    }

    @Override
    public void editCheckGroup(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.editCheckGroup(checkGroup);

        checkGroupDao.deleteCheckGroupAndCheckItem(checkGroup.getId());

        if (checkitemIds !=null&&checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("GroupId",checkGroup.getId());
                map.put("ItemId",checkitemId);
                checkGroupDao.addCheckGroupRelation(map);
            }
        }
    }

    /**
     * 删除检查组数据
     * @param id 要删除的检查组ID
     */
    @Override
    public void delete(Integer id) {
        long count = checkGroupDao.findGroupRelation(id);
        if (count>0){

            new RuntimeException();
        }

        checkGroupDao.deleteCheckGroupAndCheckItem(id);
        checkGroupDao.deleteCheckGroup(id);
        System.out.println(333);
    }

    /**
     * 查询所有检查组数据
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = checkGroupDao.findByCondition(queryString);
        PageResult pageResult=new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }
}
